<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="角色管理">
            <el-row style="height: 60px">
              <el-button class="el-icon-plus" type="primary" size="small" @click="showDialog=true">新增角色</el-button>
            </el-row>
            <el-table border="" :data="list">
              <el-table-column header-align="left" type="index" align="center" label="序号" width="120" />
              <el-table-column prop="name" align="center" label="角色名称" width="240" />
              <el-table-column prop="description" align="center" label="描述" />
              <el-table-column align="center" label="操作">
                <template v-slot="{ row }">
                  <el-button size="small" type="success" @click="assignPerm(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRole(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="deleteRole(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <el-pagination
                :current-page="page.page"
                :page-size="page.pagesize"
                :total="page.total"
                layout="prev, pager, next"
                @current-change="changePage"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top: 50px">
              <el-form-item label="企业名称"><el-input v-model="formData.name" disabled style="width: 400px" /></el-form-item>
              <el-form-item label="公司地址"><el-input v-model="formData.companyAddress" disabled style="width: 400px" /></el-form-item>
              <el-form-item label="电话"><el-input v-model="formData.companyPhone" disabled style="width: 400px" /></el-form-item>
              <el-form-item label="邮箱"><el-input v-model="formData.mailbox" disabled style="width: 400px" /></el-form-item>
              <el-form-item label="备注"><el-input v-model="formData.remarks" type="textarea" :rows="3" disabled style="width: 400px" /></el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <el-dialog ref="roleForm" :title="roleForm.id ? '编辑角色' : '新增角色'" :visible="showDialog" @close="btnCancel">
      <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="120px">
        <el-form-item prop="name" label="角色名称">
          <el-input v-model.trim="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="btnCancel">取消</el-button>
          <el-button size="small" type="primary" @click="btnOK">确认</el-button>
        </el-col>
      </el-row>
    </el-dialog>
    <!-- 分配权限弹层 -->
    <el-dialog title="分配权限" :visible.sync="showPermDialog" @close="btnAssignCancel">
      <!-- 权限树 node-key="id"表示id作为唯一标识-->
      <el-tree
        ref="permTree"
        :data="permData"
        :props="defaultProps"
        node-key="id"
        :default-checked-keys="selectChecked"
        default-expand-all
        show-checkbox
        check-strictly
      />
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" type="primary" @click="btnAssignOK">确定</el-button>
          <el-button size="small" @click="btnAssignCancel">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { getRoleList, getCompanyInfo, deleteRole, getRoleDetail, updateRole, addRole, assignPerm } from '@/api/setting'
import { getPermissionList } from '@/api/permission'
import { tranListToTreeData } from '@/utils'
// import { mapGetters } from 'vuex'
export default {
  components: {},
  props: {},
  data() {
    return {
      list: [],
      page: {
        page: 1,
        pagesize: 10,
        total: 0
      },
      permData: [], // 所有权限数据
      formData: {}, // 公司信息
      showDialog: false,
      showPermDialog: false,
      defaultProps: {
        label: 'name' // 定义权限树型数据默认显示字段的名称
      },
      roleId: null, // 记录当前角色的id
      selectChecked: [], // 当前的角色拥有的权限点数组
      roleForm: { // 角色信息
        name: '',
        despription: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    // ...mapGetters(['companyId'])
  },
  watch: {},
  created() {
    this.getRoleList()
    this.getCompanyInfo()
  },
  methods: {
    async getRoleList() {
      const { total, rows } = await getRoleList(this.page)
      this.page.total = total
      this.list = rows
    },
    async getCompanyInfo() {
      this.formData = await getCompanyInfo(this.$store.getters.companyId)
    },
    changePage(newPage) {
      this.page.page = newPage
      this.getRoleList()
    },
    async deleteRole(id) {
      try {
        await this.$confirm('确认删除角色吗?')
        await deleteRole(id)
        this.getRoleList()
        this.$message.success('删除角色成功!')
      } catch (err) {
        console.log(err)
      }
    },
    async editRole(id) {
      this.roleForm = await getRoleDetail(id)
      this.showDialog = true
    },
    async btnOK() {
      await this.$refs.roleForm.validate()
      try {
        if (this.roleForm.id) {
          await updateRole(this.roleForm)
        } else {
          await addRole(this.roleForm)
        }
        this.getRoleList()
        this.$message.success('操作成功')
        this.showDialog = false
      } catch (err) {
        console.log(err)
      }
    },
    btnCancel() {
      this.roleForm = {
        name: '',
        despription: ''
      }
      this.$refs.roleForm.resetFields()
      this.showDialog = false
    },
    // 点击"分配权限"
    async assignPerm(id) {
      // 获取所有权限数据转化树型展示
      this.permData = tranListToTreeData(await getPermissionList(), '0')
      this.roleId = id
      // 根据当前角色id获取对应的权限数据
      const { permIds } = await getRoleDetail(id)
      this.selectChecked = permIds
      this.showPermDialog = true
    },
    btnAssignCancel() {
      this.selectChecked = []
      this.showPermDialog = false
    },
    async btnAssignOK() {
      await assignPerm({ permIds: this.$refs.permTree.getCheckedKeys(), id: this.roleId })
      this.$message.success('分配权限成功')
      this.showPermDialog = false
    }
  }
}
</script>

<style scoped lang="scss"></style>
